<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="classList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="班级ID" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.className }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 2">已删除</el-tag>
          <el-tag type="danger" v-else>状态异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" v-if="scope.row.status == 1" type="danger"
            @click="setStatusAt(scope.row)">删除</el-button>
          <el-button size="mini" v-if="scope.row.status == 2" type="success"
            @click="setStatusAt(scope.row)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination background layout="prev, pager, next" :total="requestParams.total" @current-change="change"
        :page-size="requestParams.pageSize">
      </el-pagination>
    </div>

    <!-- el-dialog -->
    <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="classForm">
        <el-form-item label="班级名字" :label-width="formLabelWidth">
          <el-input v-model="classForm.className" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :loading="buttonLoading" @click="toSub">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getClassList, updateClass, getClassById } from "@/api/class";
export default {
  data() {
    return {
      classList: null,
      listLoading: true,

      classForm: {
        className: ""
      },
      requestParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      dialogFormVisible: false,
      buttonLoading: false,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      // 假设 getClasss 是获取班级列表的API方法
      getClassList(this.requestParams).then(response => {
        this.classList = response.data.list;
        this.requestParams.total = response.data.total;
        this.listLoading = false;
      });
    },

    change(page) {
      this.requestParams.pageNo = page;
      this.fetchData();
    },

    //编辑班级
    handleEdit(row) {
      this.dialogFormVisible = true;
      console.log("编辑班级:", row);
      this.classForm.className = row.className;
      this.classForm.id = row.id;
    },

    //处理响应
    opt(res) {
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        });
      }
    },
    // 编辑的提交
    toSub(row) {

      this.buttonLoading = true;
      updateClass(this.classForm).then((res) => {
        this.opt(res)
        this.fetchData()
      });
      this.buttonLoading = false;
      this.dialogFormVisible = false;
    },
    //修改状态
    setStatus(row) {
      // this.setStatusAt()
      updateClass({ id: row.id, status: row.status }).then(res => {
        this.opt(res)
        this.fetchData()
      })
      // fetchData()
    },

    setStatusAt(row) {
      this.$confirm('修改该班级状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.setStatus(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
};
</script>

<style scoped>
.page {
  margin-top: 15px;
  text-align: center;
}

.app-container {
  padding: 20px;
}
</style>